<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 云盟保养页面
// +----------------------------------------------------------------------
namespace app\yunmeng\controller;

use app\yunmeng\model\YmCarlog as YmCasrlogModel;
use app\yunmeng\service\Jssdk;
use app\yunmeng\controller\Xiche;
use think\Controller;
use think\Db;

class Baoyang extends Controller
{
  public function index()
  {
    $jsSdk = new Jssdk();
    $json = $jsSdk->signature();
    $config = json_decode($json,true);
    $this->assign(['config' => $config]);
    return view();
  }

  // 计算距离排序
  public function paix(){
    $gps = Request()->param();
    $xiche = new Xiche();
    if ($gps) {
      $fP1Lat = $gps['lat'];
      $fP1Lon = $gps['lng'];
      $baoyang = Db::name('ym_xiche')->where('type','=',1)->select();
      foreach($baoyang as $k => $v)
      {
        $gps = explode(",", $v['zuob']);
        $fP2Lat = $gps[0];
        $fP2Lon = $gps[1];
        $juli = $xiche->distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon);
        $baoyang[$k]['sort'] = $juli;
        $baoyang[$k]['pic'] = getUrl($v['pic']);
      }
      foreach($baoyang as $k => $v)
      {
        $sort[$k] = $v['sort'];
      }
      array_multisort($sort,$baoyang);
      return json($baoyang);
    } else {
      return $this->error('定位失败');
    }
  }

  // 保养价格页面
  public function baoy($id)
  {
    if($id > 0){
      $jiyou = Db::name('ym_jiyou')->select();
      $hex = Db::name('ym_xiche')->where('id','=',$id)->find();
      $this->assign([
        'hex' => $hex,
        'jiyou' => $jiyou
      ]);
      return view();
    } else {
      $this->error('id错误');
    }
  }
}
?>